package com.mlethe.android.demo.verticaltextview;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import com.mlethe.library.widget.vertical.textview.VerticalTextView;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        VerticalTextView windCardDescTv = findViewById(R.id.windCardDescTv);
        VerticalTextView verticalTextView = findViewById(R.id.verticalTextView);
        String str = "来自###饮品猫###和###服饰猫###的生活照片。";
//        String str = "来自喵城的留影！";
        List<Integer> list = new ArrayList<>();
        StringBuilder text = getText(str, list);
        windCardDescTv.setText(text)
                .setOtherColor("#ff0000", list);
        verticalTextView.setText(text)
                .setOtherColor("#ff0000", list);
    }

    private StringBuilder getText(String str, List<Integer> list) {
        Pattern pattern = Pattern.compile("###[\\u4e00-\\u9fa5]*###");
        Matcher matcher = pattern.matcher(str);
        StringBuilder builder = new StringBuilder(str);
        int index = 0;
        while (matcher.find()) {
            String text = matcher.group();
            int start = str.indexOf(text) - index;
            int end = start + text.length();
            String substring = text.substring(text.indexOf("###") + 3, text.lastIndexOf("###"));
            builder = builder.replace(start, end, substring);
            end = start + substring.length();
            for (int i = start; i < end; i++) {
                list.add(i);
            }
            index += 6;
        }
        return builder;
    }
}